var recordFlag:Boolean=false;
var surveyUuid:String;

function init(param, data, slot)
{
	Packages.system.Platform.renderPortlet(slot);
	Packages.modula.SurveyItem.initDataProvider();
	displaySurveyList();
	setSurveyUuid();
}

function setSurveyUuid()
{
	var item=new Object();
	item.type="generateUuid";
	item.param=new Object();
	item.result=new Object();
	item.result.owner=this;
	item.result.onResult=function(res)
	{
		trace("setSurveyUuid.onResult.obj.returnObj: "+res.returnObj);
		if (res.returnObj!=null)
		{
			surveyUuid=res.returnObj;
		}
	}	
	Packages.system.MLF.rtmpCaller(item);	
}

function check(param, data, slot)
{
	trace("checkit...param:"+param+" data:"+data+" slot:"+slot);
	var idStr:String=param.substring(param.indexOf("checkit_")+8);
	var idArr:Array=idStr.split("_");
	var id:Number=idArr[0];
	var ansId:String=idArr[1];

	var check=mlf.gatekeeper.native.Caller.scanElement("SurveyPaper", "check_"+idStr); 
	var dp:Array=Packages.modula.SurveyItem.getDataProvider();
	//var checkFlag:Boolean=dp[id].answer[ansId].selected;
	//trace("before check...idStr:"+idStr+" checkFlag:"+dp[id].answer[ansId].selected);
	if (dp[id].answer[ansId].selected)
	{
		check._alpha=0;
		dp[id].answer[ansId].selected=false;
	}
	else
	{
		check._alpha=100;
		dp[id].answer[ansId].selected=true;
	}	
	//trace("after check...idStr:"+idStr+" checkFlag:"+dp[id].answer[ansId].selected);
	if (!dp[id].allowMulti)
	{
		for (var a in dp[id].answer)
		{
			if (a!=ansId&&dp[id].answer[a].selected)
			{
				dp[id].answer[a].selected=false;
				var disable=mlf.gatekeeper.native.Caller.scanElement("SurveyPaper", "check_"+id+"_"+a); 
				disable._alpha=0;
			}
		}
	}
	Packages.modula.SurveyItem.setDataProvider(dp);
}

function displayItemPortlet(cont:String)
{
	mlf.gatekeeper.native.Caller.MLoader(null, null, null, null, cont);
}

function displaySurveyList()
{
	var dp:Array=Packages.modula.SurveyItem.getDataProvider();
	for (var i=1; i<dp.length; i++)
	{
		displayItemPortlet(Packages.modula.SurveyItem.getSurveyItem(dp[i]));
	}
}

function record(param, data, slot)
{
	var mic=mlf.gatekeeper.native.Caller.scanElement("SurveyPaper", "mic"); 
	var hint=mlf.gatekeeper.native.Caller.scanElement("SurveyPaper", "recordHint"); 
	if (recordFlag)
	{
		Packages.modula.SurveyItem.close();
		recordFlag=false;
		mic._alpha=0;
		hint.htmlText=Packages.system.MLF.scanDictionary(hint, "Record...");
	}
	else
	{
		Packages.modula.SurveyItem.publish(surveyUuid);	
		recordFlag=true;
		mic._alpha=100;
		hint.htmlText=Packages.system.MLF.scanDictionary(hint, "Stop");
	}
}

function confirm(param, data, slot)
{
	var userInfo:String="";
	for (var a in data)
	{
		if (data[a]!=null&&data[a]!="")
			userInfo+=a+"="+data[a]+"&";	
	}
	
	var item=new Object();
	item.type="saveSurvey";
	item.param=new Object();
	item.param.uuid=surveyUuid;
	item.param.content=userInfo+Packages.modula.SurveyItem.getSurveyString();
	for (var a in item.param) trace("item.param."+a+":"+item.param[a]);
	item.result=new Object();
	item.result.owner=this;
	item.result.onResult=function(res)
	{
		trace("setSurveyUuid.onResult.obj.returnCode: "+res.returnCode);
		if (res.returnCode!=null)
		{
			surveyUuid=res.returnObj;
			// if submit successful
			mlf.mcmanager.SMCManager.popInstance("SurveyPaper");
		}
	}	
	Packages.system.MLF.rtmpCaller(item);	

}

function clearComment(param, data, slot)
{
	trace("clearComment...param:"+param);
	var comment=mlf.gatekeeper.native.Caller.scanElement("SurveyPaper", "commentInput");
	comment.htmlText="";
}

function rolloverButton(param, data, slot)
{
	//trace("rolloverButton...param:"+param);
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=false;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 50);	
}

function rolloutButton(param, data, slot)
{
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=true;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 0);	
}

